package com.fenghongzhang.day009;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * 内容和指定数据库关联
 */
public class UserContentProvider extends ContentProvider {

    private SQLiteDatabase db;

    public UserContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int student = db.delete("student", selection, selectionArgs);
        return student;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
         db.insert("student",null,values);
         return uri;
    }

    //和数据库建立联系
    @Override
    public boolean onCreate() {
        //db 才能对数据增删改查
        MySqlHelp mySqlHelp = new MySqlHelp(getContext(), "user.db", null, 1);
         db = mySqlHelp.getReadableDatabase();
         if(db != null){
             return true;
         }
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor student = db.query("student", projection, selection, selectionArgs, null, null, sortOrder);
        return student;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int student = db.update("student", values, selection, selectionArgs);
        return student;
    }
}
